Zepto(function ($) {
  //http://dev-bucket.bingstar.com.cn/uploadImage/2017/4/24/1492998799456.png
  var ordergoodsid = sessionStorage.getItem("ordergoodsid");
  var refund_amount = sessionStorage.getItem("refund_amount");
  var after_sale_type = sessionStorage.getItem("after_sale_type");
  var user_upload_images = "";//图片路径
  var user_remark = "";//用户备注
  var goodsState = "1";//商品状态，默认申请退款中
  window.onload=function () {
    getInitConfig();
  }
  $(".nav-header").on("click", ".back", function (e) {
    window.history.go(-1);
  });
  /**
   * 选择规格
   *
   */
  $(".specifi-type").on("click",".specifi-type-every",function () {
    $(".specifi-type-every").removeClass("active");
    $(this).addClass("active");
    user_remark = $(this)[0].getAttribute("user-remark");
    console.log(user_remark);
  });
  /**
   * 点击选择图片
   *
   */
  $(".selectimg").click(function () {
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        var localIds = res.localIds[0]; // 返回选定照片的本地ID列表，localId可以作为img标签的src属性显示图片
        viwImages(localIds);
        wx.getLocalImgData({
          localId: localIds, // 图片的localID
          success: function (res) {
            var localData = res.localData; // localData是图片的base64数据，可以用img标签显示
            ajaxUpload(localData);
          }
        });
      }
    });
  });
  function viwImages(url) {
    var table = document.body.querySelector('.specifi-type1');
    var addimg = document.createElement("div");
    addimg.className="addimg";
    addimg.innerHTML='<img src="'+url+'">'
    table.appendChild(addimg);
  }
  function ajaxUpload(localData) {
    var blobBin = dataURLtoBlob(localData);
    var oMyForm = new FormData();
    oMyForm.append("file", blobBin);
    $.ajax({
      type: 'POST',
      url: getUrl + '/wxmall/displayImge/uploadImg',
      data: oMyForm,
      dataType: 'json',
      timeout: 15000,
      headers: headers("displayImge.uploadImg"),
      async: false,
      cache: false,
      contentType: false,
      processData: false,
      success: function(data){
        uploadBefo(data);
      },
      error: function(xhr, type){
      }
    });
  }
  function uploadBefo(data) {
    if(data.success){
      if(user_upload_images == ""){
        user_upload_images = data.data
      }else {
        user_upload_images = user_upload_images  +"," + data.data;
      }
    }else{
      mui.toast(data.msg);
    }
  }
  //**dataURL to blob**  dataURL转换blob
  function dataURLtoBlob(dataurl) {
    var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1],
        bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
    while (n--) {
      u8arr[n] = bstr.charCodeAt(n);
    }
    return new Blob([u8arr], { type: mime });
  }
  /**
   * 提交申请
   *
   */
  $(".comfire-tocart").click(function () {
    var a = chenk();
    if(a){
      save();
    }
  });
  function chenk() {
    if(user_remark == ""){
      mui.toast("请选择申请理由");
      return false;
    }
    if(user_upload_images == ""){
      mui.toast("请上传商品图片");
      return false;
    }
    return true;
  }
  function save() {
    var data = {
      "orderGoodsId":ordergoodsid,
      "refundAmount":refund_amount,
      "afterSaleType":after_sale_type,
      "userUploadImages":user_upload_images,
      "userRemark":user_remark,
      "goodsState":goodsState
    }
    $.ajax({
      type: 'POST',
      url: getUrl + '/wxmall/afterSale/afterSaleApply',
      data: data,
      dataType: 'json',
      timeout: 15000,
      headers: headers("afterSale.afterSaleApply"),
      // contentType:'application/json',
      success: function(data){
        dispose(data);
      },
      error: function(xhr, type){
      }
    });
  }
  function dispose(data) {
    if(data.success){
      // window.history.go(-1);
      mui.toast(data.msg);
    }else {
      mui.toast(data.msg);
    }
  }
});